<?php  
function cpx_get_coupon_detail_3($coupon_type,$per_page,$no_page = ''){
                    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                    $date = date('Y/m/d');
                    if($coupon_type == ''){
                            $meta_query = array(
                                array('key'=>'active_metadata', 'value'=>'active', 'compare' => '=')
                                ,array('key'=>'expire_date_metadata', 'value'=>$date, 'compare' => '>')
                            );
                    }else{
                        if($coupon_type == 'code'){
                            $meta_query = array(
                                array('key'=>'active_metadata', 'value'=>'active', 'compare' => '=')
//                              ,array('key'=>'expire_date_metadata', 'value'=>$date, 'compare' => '>')
                                ,array('key'=>'coupon_type_metadata', 'value'=>$coupon_type, 'compare' => '=')
                            );
                        }else{
                            $meta_query = array(
                                array('key'=>'active_metadata', 'value'=>'active', 'compare' => '=')
//                              ,array('key'=>'expire_date_metadata', 'value'=>$date, 'compare' => '>')
                                ,array('key'=>'exclusive_metadata', 'value'=>1, 'compare' => '=')
                            );
                        }
                        
                    }
                    $args = array(
                        'post_type' => 'coupon',
                        'post_status' => 'publish',
                        'meta_query' => $meta_query,
                        'meta_key' => 'cp_rate_metadata',
                        'orderby' => 'meta_value_num',
                        'order' => 'DESC'
                    );
                    if($per_page == ''){
                        $args['posts_per_page'] = $no_page;
                    }
                    if($per_page != ''){
                        $args['posts_per_page'] = $per_page;
                        $args['paged'] = $paged;
                    }
                    $query = new WP_Query($args);
                    $total_pages = $query->max_num_pages;
                    if($query->have_posts())
                    { ?>
                        <div class="row-fluid boxf">
                            <div class="row-fluid top-rate header-coupon">
                                <div class="span12 code-coupon">
                                    <h1><?php echo 'Top Coupons'; ?></h1>
                                </div>
                            </div>
                       <?php 
                            foreach ($query->posts as $v) {
                                $cp_id = $v->ID;
                                cpx_get_coupon_detail_2($cp_id);
                            }
                       ?>
                                <?php 
                                    if($per_page != ''){ 
                                        pagination($total_pages,2); 
                                    }
                                ?>
                        </div>
                <?php }
                if($total_pages == 0){?>
                    <div class="row-fluid boxf tax-store-category">
                        <div class="row-fluid header-coupon">
                             <div class="span12 code-coupon"><h2>Most coupon say: </h2></div>
                        </div>
                        <div class="row-fluid notfound">
                            <span>Sorry, we cannot find this page.</span>
                            <p>However, we suggest these coupons.</p>
                        </div>
                        <?php 
                            $page = get_option('pagination_404');
                            cpx_get_coupon_detail_3('','',$page);
                        ?>
                    </div>
                <?php }
}
?>